package com.devouty.mp3online.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import java.util.List;

/**
 * Created by devouty on 15年7月31日.
 */
public class MusicService extends Service{
    public static final String KEY_COMMAND = "k_command";
    public static  final String KEY_MUSIC_NAME = "k_music_name";
    public static  final String KEY_MUSIC_LIST = "k_music_list";
    public static  final String KEY_MUSIC_INDEX = "k_music_index";
    public static  final String MUSIC_TIME_TOTAL = "music_time_total";
    public static  final String MUSIC_TIME_CURR = "music_time_curr";
    public static  final String CAST_ACTION_UPDATE = "com.devouty.mp3online.MUSIC_TIME_UPDATE";

    public static final int CMD_INIT = 1000;
    public static final int CMD_PLAY = 1001;
    public static final int CMD_PAUSE = 1002;
    public static final int CMD_NEXT = 1003;
    public static final int CMD_PREV = 1004;
    public static final int CMD_STOP = 1005;
    public static final int CMD_RESUME = 1006;
    private MusicUtil mMusicUtil;
    private Context mContext;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int command = intent.getIntExtra(KEY_COMMAND,-1);
        switch (command)
        {
            case CMD_INIT:
                List<String> musicList = intent.getStringArrayListExtra(KEY_MUSIC_LIST);
                mMusicUtil = new MusicUtil(mContext, musicList);
                break;
            case CMD_PLAY:
                int index = intent.getIntExtra(KEY_MUSIC_INDEX, 0);
                mMusicUtil.play(index);
                break;
            case CMD_PAUSE:
                mMusicUtil.pause();
                break;
            case CMD_RESUME:
                mMusicUtil.play();
                break;
            case CMD_NEXT:
                mMusicUtil.next();
                break;
            case CMD_PREV:
                mMusicUtil.prev();
                break;
            case CMD_STOP:
                mMusicUtil.stop();
                break;
            default:break;
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
